//
//  PlanSelectPanelView.m
//  TestMoveView
//
//  Created by yull on 14/12/3.
//  Copyright (c) 2014年 yull. All rights reserved.
//

#import "PlanSelectPanelView.h"
#import "UIView+Frame.h"

@interface PlanSelectPanelView ()

@property (nonatomic, strong) UIImageView *moveImageView;
@property (nonatomic, assign) CGPoint movePoint;

@property (nonatomic, strong) NSMutableArray *imageViewArray;

@end

@implementation PlanSelectPanelView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = [UIColor clearColor];
        
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(5, self.height - 1.0f, self.width -10, 1.0f)];
        lineView.backgroundColor = RGB(239, 193, 153);
        [self addSubview:lineView];
        
        self.moveImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
        self.moveImageView.hidden = YES;
        [self addSubview:self.moveImageView];
        
        self.movePoint = CGPointZero;
    }
    return self;
}

- (void)reloadData
{
    if (self.imageViewArray) {
        [self.imageViewArray removeAllObjects];
    }else {
        self.imageViewArray = [NSMutableArray array];
    }
    
    for (NSInteger i = 0;i<self.imageArray.count;i++) {
        
        NSString *imageName = [self.imageArray objectAtIndex:i];
        
        CGFloat spacing = 30;
        CGFloat width = (self.width - 40 - 3*spacing)/4;
        
        CGRect rect = CGRectMake(20 + i % 4 * (width + spacing), 10 + i/4 * (width + 10), width, width);
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
        imageView.frame = rect;
        imageView.userInteractionEnabled = YES;
        [self addSubview:imageView];
        
        [self.imageViewArray addObject:imageView];
        
        UILongPressGestureRecognizer *longGes = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(onLongPress:)];
        longGes.minimumPressDuration = 0.1f;
        [imageView addGestureRecognizer:longGes];
    }
    
    self.height = [(UIView *)[self.imageViewArray lastObject] bottom] + 10;
}

- (void)onLongPress:(UILongPressGestureRecognizer *)longPress
{
    UIImageView *imageView = (UIImageView *)longPress.view;
    NSInteger index = [self.imageViewArray indexOfObject:imageView];
    UIGestureRecognizerState state = longPress.state;
    if (state == UIGestureRecognizerStateBegan) {
        
        self.moveImageView.image = imageView.image;
        self.moveImageView.frame = imageView.frame;
        self.moveImageView.hidden = NO;
        self.movePoint = [longPress locationInView:self];
        
        imageView.image = [UIImage imageNamed:[self.selectImageArray objectAtIndex:index]];
        
    }else if (state == UIGestureRecognizerStateChanged) {
        
        CGPoint point = [longPress locationInView:self];
        
        CGPoint centerPoint = self.moveImageView.center;
        centerPoint.x += (point.x - self.movePoint.x);
        centerPoint.y += (point.y - self.movePoint.y);
        self.moveImageView.center = centerPoint;
        
        self.movePoint = point;
    }else {
        imageView.image = [UIImage imageNamed:[self.imageArray objectAtIndex:index]];
        
        self.movePoint = CGPointZero;
        
        if ([self.delegate respondsToSelector:@selector(moveEndPoint:selectIndex:)]) {
            [self.delegate moveEndPoint:self.moveImageView.center selectIndex:index];
        }
        self.moveImageView.image = nil;
        self.moveImageView.hidden = YES;
    }
}
@end
